//
//  LPZWYCommonClassTableViewCell.m
//  ZhongWangYuan
//
//  Created by liuyingping on 2018/6/2.
//  Copyright © 2018年 ZJ. All rights reserved.
//

#import "LPZWYCommonClassTableViewCell.h"
#define TopGreen @"5bb85d"
#define TopOrange @"ffa016"
@interface LPZWYCommonClassTableViewCell ()
/**    */
@property (strong, nonatomic) TenGrayShadowView  *ten;
/**  图片  */
@property (strong, nonatomic) UIImageView  *leftImageView;
/**  时间  */
@property (strong, nonatomic) UIButton  *timebutton;
/**    */
@property (strong, nonatomic) UIView  *backClassview;

/**  课程类型图标  */
@property (strong, nonatomic) UIImageView  *classTypeImageView;
/**  title  */
@property (strong, nonatomic) UILabel  *titleLabel;
/**    */
@property (strong, nonatomic) UIView  *bottomSlider;
/**    */
@property (strong, nonatomic)  UIView *topSlider;
/**    */
@property (strong, nonatomic) UILabel  *baifenbi;
/**  主讲人  */
@property (strong, nonatomic) UILabel  *speakPerson;


/**    */
@property (strong, nonatomic) UIView  *eye;
/**    */
@property (strong, nonatomic) UILabel  *eyeLabel;

/**    */
@property (strong, nonatomic) UIView  *star;
/**    */
@property (strong, nonatomic) UILabel  *starLabel;

/**    */
@property (strong, nonatomic) UIView  *book;
/**    */
@property (strong, nonatomic) UILabel  *bookLabel;


@end

// 125
@implementation LPZWYCommonClassTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}


- (void)layoutUI{
    [self addSubview:self.ten];
    [self addSubview:self.leftImageView];
    [self.leftImageView addSubview:self.timebutton];
    [self.leftImageView addSubview:self.backClassview];
    [self.leftImageView addSubview:self.classTypeImageView];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.bottomSlider];
    [self.bottomSlider addSubview:self.topSlider];
    [self addSubview:self.baifenbi];
    [self addSubview:self.eye];
    [self addSubview:self.star];
    [self addSubview:self.book];
     [self addSubview:self.speakPerson];
    
    
    self.ten.sd_layout
    .leftEqualToView(self)
    .topEqualToView(self)
    .rightEqualToView(self)
    .heightIs(7 * [UIScreen zj_screenHScale]);
    
    self.leftImageView.sd_layout
    .leftSpaceToView(self, 15 * [UIScreen zj_screenWScale])
    .topSpaceToView(self.ten, 15 * [UIScreen zj_screenHScale])
    .heightIs(90 * [UIScreen zj_screenHScale])
    .widthIs(130 * [UIScreen zj_screenWScale]);
    
    self.timebutton.sd_layout
    .leftEqualToView(self.leftImageView)
    .leftEqualToView(self.leftImageView)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .widthIs(75 * [UIScreen zj_screenWScale]);
    
    self.backClassview.sd_layout
    .centerXEqualToView(self.leftImageView)
    .centerYEqualToView(self.leftImageView)
    .heightIs(32 * [UIScreen zj_screenHScale])
    .widthIs(32 * [UIScreen zj_screenHScale]);
    
    self.classTypeImageView.sd_layout
    .centerXEqualToView(self.leftImageView)
    .centerYEqualToView(self.leftImageView)
    .heightIs(28 * [UIScreen zj_screenHScale])
    .widthIs(28 * [UIScreen zj_screenHScale]);
    
    self.eye.sd_layout
    .leftSpaceToView(self.leftImageView, 10 * [UIScreen zj_screenWScale])
    .bottomEqualToView(self.leftImageView)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .widthIs(([UIScreen zj_screenWith] - 175 * [UIScreen zj_screenWScale]) / 3);
    [self.eye addSubview:self.eyeLabel];
    
    self.eyeLabel.sd_layout
    .rightEqualToView(self.eye)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .topEqualToView(self.eye)
    .widthIs(self.eye.width - 18 * [UIScreen zj_screenWScale]);
    
    
    self.star.sd_layout
    .leftSpaceToView(self.eye, 5 * [UIScreen zj_screenWScale])
    .bottomEqualToView(self.eye)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .widthIs(([UIScreen zj_screenWith] - 175 * [UIScreen zj_screenWScale]) / 3);
    [self.star addSubview:self.starLabel];
    self.starLabel.sd_layout
    .rightEqualToView(self.star)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .topEqualToView(self.star)
    .widthIs(self.star.width - 18 * [UIScreen zj_screenWScale]);
    
    self.book.sd_layout
    .leftSpaceToView(self.star, 5 * [UIScreen zj_screenWScale])
    .bottomEqualToView(self.eye)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .widthIs(([UIScreen zj_screenWith] - 175 * [UIScreen zj_screenWScale]) / 3);
    [self.book addSubview:self.bookLabel];
    self.bookLabel.sd_layout
    .rightEqualToView(self.book)
    .heightIs(15 * [UIScreen zj_screenHScale])
    .topEqualToView(self.book)
    .widthIs(self.book.width - 18 * [UIScreen zj_screenWScale]);
    
    self.baifenbi.sd_layout
    .rightSpaceToView(self, 10 * [UIScreen zj_screenWScale])
    .heightIs(10 * [UIScreen zj_screenHScale])
    .widthIs(40 * [UIScreen zj_screenWScale])
    .bottomSpaceToView(self.eye, 5 * [UIScreen zj_screenHScale]);
    
    self.bottomSlider.sd_layout
    .leftEqualToView(self.eye)
    .rightSpaceToView(self.baifenbi, 5 * [UIScreen zj_screenWScale])
    .bottomEqualToView(self.baifenbi)
    .heightIs(10 * [UIScreen zj_screenHScale]);
    
    self.topSlider.sd_layout
    .leftEqualToView(self.bottomSlider)
    .topEqualToView(self.bottomSlider)
    .widthIs(100)
    .heightIs(10 * [UIScreen zj_screenHScale]);
    
    
  
    self.titleLabel.sd_layout
    .leftEqualToView(self.eye)
    .topEqualToView(self.leftImageView)
    .heightIs(35 * [UIScreen zj_screenHScale])
    .widthIs([UIScreen zj_screenWith] - 165 * [UIScreen zj_screenWScale]);
    
    
    self.speakPerson.sd_layout
    .leftEqualToView(self.eye)
    .topSpaceToView(self.titleLabel, 8 * [UIScreen zj_screenHScale])
    .heightIs(12 * [UIScreen zj_screenHScale])
    .widthIs([UIScreen zj_screenWith] - 165 * [UIScreen zj_screenWScale]);
    
    
   
    
}

- (void)setModel:(LPZWYChooseClassClassModel *)model{
    _model = model;
    if (model.name.length != 0) {
         self.titleLabel.text = model.name;
    }else{
         self.titleLabel.text = model.coursewarename;
    }
   
    CGFloat height = [self.titleLabel getLableSizeWithMaxWidth:self.titleLabel.width].height;
    
    if (height < 35 * [UIScreen zj_screenHScale]) {
        self.titleLabel.sd_layout.heightIs(height);
        [self.titleLabel updateLayout];
    }
    
    
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@" , CREATEURL(model.imagephoto)]] placeholderImage:PlaceholderImage];
    
    
    self.classTypeImageView.hidden = NO;
    self.backClassview.hidden = NO;
    if (model.teachervideo.length != 0 && model.audio_url.length != 0) {
        self.classTypeImageView.image = [UIImage imageNamed:@"list_li_pl"];
    }else if (model.teachervideo.length != 0 && model.audio_url.length == 0) {
        self.classTypeImageView.image = [UIImage imageNamed:@"list_play"];
    }else if (model.teachervideo.length == 0 && model.audio_url.length != 0){
        self.classTypeImageView.image = [UIImage imageNamed:@"list_li_pl"];
    }else{
        self.classTypeImageView.hidden = YES;
        self.backClassview.hidden = YES;
    }
    if (model.realduration.length != 0) {
        [self.timebutton setTitle:[NSString stringWithFormat:@" %@", model.realduration] forState:UIControlStateNormal];
    }else{
        NSInteger timeBumber = [model.duration integerValue];
        if (self.isMintue) {
            timeBumber *= 60;
        }
        NSString *time = [NSString calculateTimeWithTimeFormatter:timeBumber];
         [self.timebutton setTitle:[NSString stringWithFormat:@" %@",time] forState:UIControlStateNormal];
    }
   
    
    NSString *speaking;
    if (!model.teachersname) {
        speaking = @"";
    }else if (!model.Description){
        speaking = [NSString stringWithFormat:@"主讲人：%@",model.teachersname];
    }else{
        speaking = [NSString stringWithFormat:@"主讲人：%@(%@)",model.teachersname,model.Description];
    }
    self.speakPerson.text = speaking;
    
    self.eyeLabel.text = model.clickrate;
    self.starLabel.text = [NSString stringWithFormat:@"%.2f",[model.grade floatValue]];
    self.bookLabel.text = [NSString stringWithFormat:@"%.1f",[model.studytime floatValue]];
    
    
    /**  计算出每百分之一所占比例  */
    CGFloat bilie =  ([UIScreen zj_screenWith] - 210 * [UIScreen zj_screenWScale]) / 100;
    self.baifenbi.text = model.playpercentage;
    NSRange range = [model.playpercentage rangeOfString:@"%"];
    NSString *subSring = [model.playpercentage substringToIndex:range.location];
    
    
    if ([subSring integerValue] == 0) {
        self.topSlider.hidden = YES;
         self.baifenbi.textColor = [UIColor zj_colorWithHexString:TopOrange];
    }else if ([subSring integerValue] == 100){
        self.topSlider.hidden = NO;
        self.topSlider.backgroundColor = [UIColor zj_colorWithHexString:TopGreen];
        self.baifenbi.textColor =  [UIColor zj_colorWithHexString:TopGreen];
        self.topSlider.sd_layout
        .widthIs(bilie * 100);
        [self.topSlider updateLayout];
    }else{
        self.topSlider.hidden = NO;
         self.topSlider.backgroundColor = [UIColor zj_colorWithHexString:TopOrange];
         self.baifenbi.textColor = [UIColor zj_colorWithHexString:TopOrange];
        
        self.topSlider.sd_layout
        .widthIs(bilie * [subSring integerValue]);
        [self.topSlider updateLayout];
    }
    
    if (YBUSERINFOMODEL.studentId.length == 0) {
        self.bottomSlider.hidden = YES;
        self.baifenbi.hidden = YES;
    }
    
}

#pragma mark - setter

- (TenGrayShadowView *)ten{
    if (_ten == nil) {
        _ten = [TenGrayShadowView new];
        
    }
    return _ten;
}

- (UIImageView *)leftImageView{
    if (_leftImageView == nil) {
        _leftImageView = [UIImageView new];
        _leftImageView.layer.masksToBounds = YES;
        _leftImageView.layer.cornerRadius = 4;
        
    }
    return _leftImageView;
}

- (UIImageView *)classTypeImageView{
    if (!_classTypeImageView) {
        _classTypeImageView = [UIImageView new];
        _classTypeImageView.contentMode = UIViewContentModeCenter;
    }
    return _classTypeImageView;
}

- (UIView *)backClassview{
    if (_backClassview == nil) {
        _backClassview = [UIView new];
        _backClassview.backgroundColor = [UIColor whiteColor];
        _backClassview.alpha = 0.4;
        _backClassview.layer.masksToBounds = YES;
        _backClassview.layer.cornerRadius = 16 * [UIScreen zj_screenHScale];
        
    }
    return _backClassview;
}
- (UIButton *)timebutton{
    if (_timebutton == nil) {
        _timebutton = [UIButton new];
        _timebutton.backgroundColor = [UIColor zj_colorWithHexString:@"353a34" alpha:0.5];
        [_timebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_timebutton setImage:[UIImage imageNamed:@"list_ac"] forState:UIControlStateNormal];
        _timebutton.titleLabel.font = LYPFONT(11);
        
    }
    return _timebutton;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = LYPFONT(14);
        _titleLabel.numberOfLines = 2;
        _titleLabel.textColor = [UIColor textGrayColor];
        
    }
    return _titleLabel;
}

- (UIView *)bottomSlider{
    if (!_bottomSlider) {
        _bottomSlider = [UIView new];
        _bottomSlider.backgroundColor = [UIColor themeGrayColor];
        _bottomSlider.layer.masksToBounds = YES;
        _bottomSlider.layer.cornerRadius = 5;
        
    }
    return _bottomSlider;
}


- (UIView *)topSlider{
    if (!_topSlider) {
        _topSlider = [UIView new];
        _topSlider.backgroundColor = [UIColor zj_colorWithHexString:TopOrange];
        _topSlider.layer.masksToBounds = YES;
        _topSlider.layer.cornerRadius = 5;
        
    }
    return _topSlider;
}

- (UILabel *)baifenbi{
    if (!_baifenbi) {
        _baifenbi = [UILabel new];
        _baifenbi.textAlignment = NSTextAlignmentCenter;
        _baifenbi.font = LYPFONT(14);
    }
    return _baifenbi;
}

- (UIView *)eye{
    if (!_eye) {
        _eye = [UIView new];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15 * [UIScreen zj_screenHScale], 15 * [UIScreen zj_screenHScale])];
        image.image = [UIImage imageNamed:@"list_view"];
        image.contentMode = UIViewContentModeCenter;
        [_eye addSubview:image];
      
        
    }
    return _eye;
}

- (UILabel *)eyeLabel{
    if (!_eyeLabel) {
        _eyeLabel = [UILabel new];
        _eyeLabel.font = LYPFONT(12);
        _eyeLabel.textColor = [UIColor lightGrayColor];
    }
    return _eyeLabel;
}

- (UIView *)star{
    if (!_star) {
        _star = [UIView new];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15 * [UIScreen zj_screenHScale], 15 * [UIScreen zj_screenHScale])];
        image.contentMode = UIViewContentModeCenter;
        image.image = [UIImage imageNamed:@"list_C"];
        [_star addSubview:image];
        
    }
    return _star;
}

- (UILabel *)starLabel{
    if (!_starLabel) {
        _starLabel = [UILabel new];
        _starLabel.font = LYPFONT(12);
        _starLabel.textColor = [UIColor lightGrayColor];
    }
    return _starLabel;
}

- (UIView *)book{
    if (!_book) {
        _book = [UIView new];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15 * [UIScreen zj_screenHScale], 15 * [UIScreen zj_screenHScale])];
        image.image = [UIImage imageNamed:@"list_rt"];
        image.contentMode = UIViewContentModeCenter;
        [_book addSubview:image];
        
    }
    return _book;
}

- (UILabel *)bookLabel{
    if (!_bookLabel) {
        _bookLabel = [UILabel new];
        _bookLabel.font = LYPFONT(12);
        _bookLabel.textColor = [UIColor lightGrayColor];
    }
    return _bookLabel;
}

- (UILabel *)speakPerson{
    if (!_speakPerson) {
        _speakPerson = [UILabel new];
        _speakPerson.textColor = [UIColor lightGrayColor];
        _speakPerson.numberOfLines = 2;
        _speakPerson.font = LYPFONT(13);
    }
    return _speakPerson;
}

@end
